﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using HalconDotNet;
using Project1.Interface;
using Project1.Common;
namespace Project1.Element
{
    public class ROI : IROI
    {
        public delegate void CallbackMehod(HTuple windowHandle);
        private CallbackMehod callbackMethod;
        private HObject contours, region;
        private int type;
        private Hashtable args;
     
        public CallbackMehod CallBackFunc
        {
            set { callbackMethod = value; }
            }

        public void CallBack(HTuple windowHandle)
        {
            if (callbackMethod != null) callbackMethod(windowHandle);
        }
        public HObject Contours() 
        {
            if(contours == null)
            {
                switch (type)
                {
                    case ConstValue.ROI_CIRCLE:
                        //HTuple area, row, column;
                        HOperatorSet.GenCircle(out region, (HTuple)args[ConstValue.CIRCLE_ROW], (HTuple)args[ConstValue.CIRCLE_COLUMN], (HTuple)args[ConstValue.CIRCLE_RADIUS]);
                        //HOperatorSet.AreaCenter(region, out area, out row, out column);
                        HOperatorSet.GenContourRegionXld(region, out contours, "border");
                        break;
                    case ConstValue.ROI_LINE:
                        HTuple beginRow = (HTuple)args[ConstValue.LINE_BEGIN_ROW];
                        HTuple beginColumn = (HTuple)args[ConstValue.LINE_BEGIN_COLUMN];
                        HTuple endRow = (HTuple)args[ConstValue.LINE_END_ROW];
                        HTuple endColumn = (HTuple)args[ConstValue.LINE_END_COLUMN];
                        HOperatorSet.GenContourPolygonXld(out contours, beginRow.TupleConcat(endRow), beginColumn.TupleConcat(endColumn));
                        HOperatorSet.GenRegionLine(out region, beginRow, beginColumn, endRow, endColumn);
                        break;
                    case ConstValue.ROI_RECTANGLE:
                        HOperatorSet.GenRectangle2(out region, (HTuple)args[ConstValue.RECT_ROW], (HTuple)args[ConstValue.RECT_COLUMN], (HTuple)args[ConstValue.RECT_PHI], (HTuple)args[ConstValue.RECT_LEN1], (HTuple)args[ConstValue.RECT_LEN2]);
                        HOperatorSet.GenContourRegionXld(region, out contours, "border");
                        break;
                }
            }
          
            return contours;
        }




        public void Dispose()
        {
            if (contours != null) contours.Dispose();
            if (region != null) region.Dispose();
        }

        public ROI(HObject region,Hashtable args,int type)
        {
            IDictionaryEnumerator ie = args.GetEnumerator();
            this.args = new Hashtable();
            this.type = type;
            while (ie.MoveNext())
                this.args.Add(ie.Key, ie.Value);
            HObject skeleton=null;
            this.region = new HObject(region);
            HOperatorSet.Skeleton(region, out skeleton);
            HOperatorSet.GenContoursSkeletonXld(skeleton, out contours, 1,"filter");
      
        }

        public ROI(HObject contours,HObject region,Hashtable args,int type)
        {
            IDictionaryEnumerator ie = args.GetEnumerator();
            this.args = new Hashtable();
            while (ie.MoveNext())
                this.args.Add(ie.Key, ie.Value);
            this.contours = new HObject(contours);
            this.region = new HObject(region);
        }

        public ROI(HObject contours, HObject region, CallbackMehod callbackMethod,Hashtable args,int type)
        {
            IDictionaryEnumerator ie = args.GetEnumerator();
            this.args = new Hashtable();
            while (ie.MoveNext())
                this.args.Add(ie.Key, ie.Value);
            this.contours = new HObject(contours);
            this.region = new HObject(region);
            this.callbackMethod = callbackMethod;
        }
        
        public ROI(ROI roi)
        {
            type = roi.type;
            if(roi.args != null)
            {
                IDictionaryEnumerator ie = roi.args.GetEnumerator();
                args = new Hashtable();
                while (ie.MoveNext())
                    this.args.Add(ie.Key, ie.Value);
            }
            if (roi.contours!=null) contours = new HObject(roi.contours);
            if (roi.region != null) region = new HObject(roi.region);
        }

        public ROI()
        {

        }

        public int Type
        {
            get { return type; }
            set { type = value; }
        }
        public Hashtable Args
        {
            get { return args; }
            set 
            {
                args = value;
            }
        }
        public HObject Region()
        {
            if (region == null)
            {
                switch (type)
                {
                    case ConstValue.ROI_CIRCLE:
                        //HTuple area, row, column;
                        HOperatorSet.GenCircle(out region, (HTuple)args[ConstValue.CIRCLE_ROW], (HTuple)args[ConstValue.CIRCLE_COLUMN], (HTuple)args[ConstValue.CIRCLE_RADIUS]);
                       // HOperatorSet.AreaCenter(region, out area, out row, out column);
                        HOperatorSet.GenContourRegionXld(region, out contours, "border");


                        break;
                    case ConstValue.ROI_LINE:
                        HTuple beginRow = (HTuple)args[ConstValue.LINE_BEGIN_ROW];
                        HTuple beginColumn = (HTuple)args[ConstValue.LINE_BEGIN_COLUMN];
                        HTuple endRow = (HTuple)args[ConstValue.LINE_END_ROW];
                        HTuple endColumn = (HTuple)args[ConstValue.LINE_END_COLUMN];
                        HOperatorSet.GenContourPolygonXld(out contours, beginRow.TupleConcat(endRow), beginColumn.TupleConcat(endColumn));
                        HOperatorSet.GenRegionLine(out region, beginRow, beginColumn, endRow, endColumn);
                        break;
                    case ConstValue.ROI_RECTANGLE:
                        HOperatorSet.GenRectangle2(out region, (HTuple)args[ConstValue.RECT_ROW], (HTuple)args[ConstValue.RECT_COLUMN], (HTuple)args[ConstValue.RECT_PHI], (HTuple)args[ConstValue.RECT_LEN1], (HTuple)args[ConstValue.RECT_LEN2]);
                        HOperatorSet.GenContourRegionXld(region, out contours, "border");
                        break;
                }
            }
            return region;
        }
      
        
       
        


    }
}
